
getCookie=function(name){
    var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr!=null)return unescape(arr[2]); return "";
}
var token= getCookie("token");
$(function(){
    var pagelimit = 10;
    function pages(count, limit){
        var page = 0;
        if(count>0){

            var page = Math.ceil(count/limit);

            var pagetag = "";
            for (var k=1;k<=page;k++) {
                if(k==1){
                    pagetag += '<li><a href="#" style="background:#ccc">'+k+'</a></li>';
                }else{
                    pagetag += '<li><a href="#" >'+k+'</a></li>';
                }
            }
            $('.pagination').html(pagetag);
        }
    }
    //左侧菜单悬停
    $(".list_menu li").hover(function(){
        var index=$(this).index()
        if(index==0||index==5||index==8){
            $(this).css('backgroundColor','#ffffff')
        }
        else {
            $(this).css('backgroundColor','#FEE631')
        }
    },function(){
        var index=$(this).index()
        if(index==4){
            $(this).css('backgroundColor','#FEE631')
        }
        else{
            $(this).css('backgroundColor','#ffffff')
        }
    })
    //	获取个人资料
    $.ajax({
        url:'http://ww.yc630.com/api/index.php/Buyer/User/userinfo',
        type:"post",
        data:{
            'token':token
        },
        dataType:"JSON",
        success:function(data){
            var avatar = data['data']['avatar'];
            if(avatar==''){
                avatar = '../img/Group%2019.png';
            }
            $("#headPic").attr('src',avatar)
        }
    })
    //获得商家信息
    $.ajax({
        type:"post",
        url:"http://ww.yc630.com/api/index.php/Buyer/Goods/sellerdetail",
        data:{
            'token':token
        },
        dataType:'json',
        success:function(data){
            $("#icon_img").attr('src',data.data.icon_image)
            $("#icon_name").html(data.data.name)
        }
    });
//    属性模板列表
        $.ajax({
            type:"post",
            url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_list",
            data:{'page':1,"pagelimit":pagelimit,"token":token},
            dataType:'json',
            success:function(phpdata){
                console.log(phpdata['data']['count'])
                pages(phpdata['data']['count'],pagelimit);
                //建立查询状态
                $('.main_right').attr('search_name','');
                $('.main_right').attr('search_value','');
                var str = '<tr class="z_head" count ="'+phpdata['data']['count']+'"><td>序号</td><td>商品模板名称</td><td>备注</td><td>属性个数</td><td>最后修改时间</td><td>操作</td></tr>';
                if(phpdata['data']['count']==0){
                    str += '<tr><td colspan="6" style="color:#999">暂无搜索结果</td></tr>';
                    $('.z_t').html(str);
                    return false;
                }
                for(i in phpdata['data']['tpls']){
                    str +='<tr class="z_list_main"><td>'+i+'</td><td>'+phpdata['data']['tpls'][i]['tpl_name']+'</td><td>'+phpdata['data']['tpls'][i]['remark']+'</td><td>'+phpdata['data']['tpls'][i]['nu']+'</td><td>'+phpdata['data']['tpls'][i]['modifytime']+'</td><td>	<a onclick="tpl_update(this)">编辑</a> <input type="hidden" value="'+phpdata['data']['tpls'][i]['id']+'"><a onclick="delte(this)">删除</a></td></tr>';
                }
                $('.z_t').html(str);
            }
        });
//    搜索
    $(".find_commit").click(function(){
        var goods=$("#goods_find").val()
        $.ajax({
            type:"post",
            url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_list",
            data:{'page':1,"pagelimit":pagelimit,"searchvalue":goods,"token":token},
            dataType:'json',
            success:function(phpdata){
                pages(phpdata['data']['count'],pagelimit);
                var str = '<tr class="z_head" count ="'+phpdata['data']['count']+'"><td>序号</td><td>商品模板名称</td><td>备注</td><td>属性个数</td><td>最后修改时间</td><td>操作</td></tr>';
                if(phpdata['data']['count']==0){
                    str += '<tr><td colspan="6" style="color:#999">暂无搜索结果</td></tr>';
                    $('.z_t').html(str);
                    return false;
                }
                for(i in phpdata['data']['tpls']){
                    str +='<tr class="z_list_main"><td>'+i+'</td><td>'+phpdata['data']['tpls'][i]['tpl_name']+'</td><td>'+phpdata['data']['tpls'][i]['remark']+'</td><td>'+phpdata['data']['tpls'][i]['nu']+'</td><td>'+phpdata['data']['tpls'][i]['modifytime']+'</td><td>	<a onclick="tpl_update(this)">编辑</a> <input type="hidden" value="'+phpdata['data']['tpls'][i]['id']+'"><a onclick="delte(this)">删除</a></td></tr>';
                }
                $('.z_t').html(str);
            }
        });
    })
//    分页点击
    $('.pagination').on('click','li a',function(){
        //获取当前页码
        var pagenum = $(this).html();
        //改变背景颜色
        $(this).css('background','#ccc');
        $(this).parents('li').siblings('li').find('a').css('background','#FFF')
        var datas = {};
        datas['page']=pagenum;
        datas['pagelimit']=pagelimit;
        datas['token']=token
        $.ajax({
            type:"post",
            url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_list",
            data:datas,
            dataType:'json',
            success:function(phpdata){
                var str = '<tr class="z_head" count ="'+phpdata['data']['count']+'"><td>序号</td><td>商品模板名称</td><td>备注</td><td>属性个数</td><td>最后修改时间</td><td>操作</td></tr>';
                if(phpdata['data']['count']==0){
                    str += '<tr><td colspan="6" style="color:#999">暂无搜索结果</td></tr>';
                    $('.z_t').html(str);
                    return false;
                }
                for(i in phpdata['data']['tpls']){
                    str +='<tr class="z_list_main"><td>'+i+'</td><td>'+phpdata['data']['tpls'][i]['tpl_name']+'</td><td>'+phpdata['data']['tpls'][i]['remark']+'</td><td>'+phpdata['data']['tpls'][i]['nu']+'</td><td>'+phpdata['data']['tpls'][i]['modifytime']+'</td><td>	<a onclick="tpl_update(this)">编辑</a> <input type="hidden" value="'+phpdata['data']['tpls'][i]['id']+'"><a onclick="delte(this)">删除</a></td></tr>';
                }
                $('.z_t').html(str);
            }
        });
    })
//编辑属性模板
})
function tpl_update(obj){
    var val=$(obj).next().val()
    window.location.href="attr_edit.html?id="+val+""
}
//删除
function delte(obj){
    $(".mask_wrap").show()
    $("#schu").show()
    $(".qd").click(function(){
        var id=$(obj).prev().val()
        $.ajax({
            type:"post",
            url:"http://ww.yc630.com/api/index.php/Seller/Belong/belong_delete",
            data:{
                "id":id,
                'token':token
            },
            dataType:'json',
            success:function(phpdata){
                $(obj).parent().parent().remove()
                window.location.reload();
            }
        })
    })
    $(".qx").click(function(){
        $(".mask_wrap").hide()
        $("#tijiao").hide()
    })
}